<template>
	<view>
		<u-cell-group>
			<u-cell-item title="快递公司" :value="currentObj.label" arrow @click="expressVisible = true"></u-cell-item>
			<u-cell-item title="快递单号" :arrow="false">
				<view class="input_box"><input v-model="dataForm.express_no" class="input" type="text"
						placeholder="请输入快递单号" /></view>
			</u-cell-item>
		</u-cell-group>
		<view class="glo_bot_main">
			<view class="glo_bot_fix">
				<view class="glo_button" :style="{ background: vuex_config.adminTheme }" @click="submit">提交</view>
			</view>
		</view>
		<u-select v-model="expressVisible" :list="expressList" @confirm="(e)=>confirm(e)"></u-select>
	</view>
</template>

<script>
	import {
		GetQueryDataList,
		UpdateModifyData,
		adminPost
	} from '@/common/http.request.js'
	export default {
		components: {},
		data() {
			return {
				expressVisible: false,
				orderNumVisible: false,
				judeg: true, //判断是否加载完快递公司
				currentObj: {
					value: '',
					label: '请选择快递公司'
				},

				expressObj: {
					value: '',
					label: '请选择单号'
				},
				order_no: '',
				dataForm: {
					express: '',
					express_no: '',
				},
				expressDetail: null,
				// m_common_express
				expressList: [{
					name: '客户自提',
					com: 'ziti'
				}],
				page: 1,

			};
		},
		onLoad(options) {
			if (options && options.order_no) {
				this.order_no = options.order_no
			}
			if (options && options.order_id) {
				this.getDetail(options.order_id)
			}
			this.logisticsPage()
		},

		methods: {
			confirm(arr) {
					this.currentObj = arr[0];
					this.expressChange(arr[0].value)
			},
			
			 expressChange(val) {
			        if (val === 'ziti') {
			          let da = new Date()
			          this.dataForm.express_no =
			            '' +
			            da.getFullYear() +
			            this.getStr(da.getMonth() + 1) +
			            this.getStr(da.getDate()) +
			            this.getStr(da.getHours()) +
			            this.getStr(da.getMinutes()) +
			            this.getStr(da.getSeconds())
			        }
			      },
			      getStr(val) {
			        if (val < 10) {
			          return '0' + val
			        } else {
			          return val
			        }
			      },
			
			logisticsPage() {
				this.$loading()
				GetQueryDataList('m_common_express', this.page, 500, {}, {
					sort: 'pk_id asc',
					fuzzy_field: undefined,
					api: true
				}).then(({
					data
				}) => {
					if (Array.isArray(data) && data[0] && this.judeg) {
						this.page += 1
						this.expressList = this.expressList.concat(data)
						this.logisticsPage()
					} else {
						this.expressList = this.expressList.reduce((pre, el) => {
							pre.push({
								label: el.name,
								value: el.com
							})
							return pre
						}, [])
						this.judeg = false
						this.$hide()
					}
				}).catch(()=>{
					this.$hide()
				})
			},
			// 根据订单id获取物流信息
			getDetail(order_id) {
				this.$loading()
				GetQueryDataList('b_order_express', 1,1, {
					fk_order_id: order_id
				}, {
					fuzzy_field: undefined
				}).then(({
					data
				}) => {
					this.expressDetail = data[0]
					this.$hide()
				})
			},
			// 提交
			submit() {
				if (!this.dataForm.express_no) {
					this.$toast({
						title: '请输入快递单号'
					});
					return;
				}
				if (!this.currentObj.value) {
					this.$toast({
						title: '请选择快递公司'
					});
					return
				}
			
				let params = {
					...this.dataForm,
					express: this.currentObj.value,
				}
				this.$loading();
				adminPost('SetOrderDelivery', {
						order_no: this.order_no,
						...params
					})
					.then(res => {
						this.$toast({
							title: '发货成功'
						});
						this.uniBack();
					})
					.catch(() => {});
			}
		}
	};
</script>

<style scoped lang="scss">
	.input_box {
		width: 100%;
		padding: 0 10rpx;

		.input {
			color: #333;
		}
	}
</style>
